import VueRouter from "vue-router";
import { Message } from 'element-ui'; // 引入Message组件
import Home from '../pages/home/home'
import High from '../pages/high/high'
import Things from '../pages/things/things'
import Other from '../pages/other/other'
import Error from '../pages/error'
import Detail from '../pages/blogDetail/detail.vue'
import Race from '../pages/race'
import AdminHome from '@/pages/adminViews/home.vue'
import Chart from '@/pages/adminViews/chart.vue'
import NoticeVue from '@/pages/promotion'
import AdminUser from '@/pages/adminViews/user.vue'
import AdminBlog from '@/pages/adminViews/blog.vue'
import AdminSecret from '@/pages/adminViews/secret.vue'
import Promotion from '@/pages/adminViews/Promotion.vue'

// 用于解决连续跳转同一个路由地址报错的问题
const originalPush=VueRouter.prototype.push;
VueRouter.prototype.push=function(location){
    return originalPush.call(this,location).catch(err=>err)
}
const router = new VueRouter({
    mode:'history',
    routes:[
        {path:'/',redirect:'/home'},
        {
            path:'/home',
            component:Home,
        },
        {
            path:'/home/detail',
            component:Detail
        },
        {
            path:'/high',
            component:High,
            
        },
        {
            path:'/high/detail',
            component:()=>import('@/components/high/secret.vue')
        },
        {
            path:'/things',
            component:Things
        },
        {
            path:'/other',
            component:Other,
            children:[
                {
                    path:'/other/blog',
                    component:()=>import('../pages/other/main_pages/blog')
                },
                {
                    path:'/other/chat',
                    component:()=>import('../pages/other/main_pages/chat')
                },
                {
                    path:'/other/comment',
                    component:()=>import('../pages/other/main_pages/comment')
                },
                {
                    path:'/other/gear',
                    component:()=>import('../pages/other/main_pages/gear')
                },
                {
                    path:'/other/shoucang',
                    component:()=>import('../pages/other/main_pages/shoucang')
                },
                {
                    path:'/other/event',
                    component:()=>import('../pages/other/main_pages/event')
                },
                {
                    path:'/other',
                    redirect:'/other/blog'
                }
            ]
        },
        // 比赛页面
        {
            path:'/race',
            component:Race,
        },
        // 后台管理页
        {
            path:'/admin/home',
            component:AdminHome,
            redirect:'/admin/home/charts',
            children:[
                {
                    path:'charts',
                    component:Chart
                },
                {
                    path:'user',
                    component:AdminUser
                },
                {
                    path:'blog',
                    component:AdminBlog
                },
                {
                    path:'secret',
                    component:AdminSecret
                },
                {
                    path:'promotion',
                    component:Promotion
                }
            ]
        },
        // 公告详情页
        {
            path:'/promotion/:id',
            component:NoticeVue
        },
        // 除上面那些之外的所有都显示错误页面
        {
            path:'*',
            component:Error
        },
    ]
})
router.beforeEach((to, from, next) => {
    // 判断用户当前状态是否为登录状态
    if (sessionStorage.getItem("user") === null) {
      // 除了首页的四个路由，其余全部拦截并提示登录
      if (to.path !== '/home') {
        // 弹出提示框，例如使用Element UI的MessageBox组件
        Message.error("未登录，请登录");
        next(false); // 跳转到登录页面
        return;
      }
    }
    if (to.path === '/user/login') {
      console.log("登录");
    }
    next(); // 继续路由跳转
  });

export default router;